﻿using System;
using System.Web.Script.Serialization;

namespace RestUpMVC.Representation
{
    /// <summary>
    /// A JSON representation of a resource.
    /// </summary>
    public class JsonRepresentation : IRepresentation
    {
        private object _resource;

        public virtual string ContentType
        {
            get { return "application/json"; }
        }

        public void SetResource(object resource)
        {
            if (resource == null) throw new ArgumentNullException("resource");
            
            _resource = resource;
        }

        /// <summary>
        /// Converts the resource into a JSON string.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown if the resource argument is null.</exception>
        /// <returns>A string representation of the resource.</returns>
        public virtual string ConvertResource()
        {
            return new JavaScriptSerializer().Serialize(_resource);
        }
    }
}